package com.dxc.intern.controller;


import com.dxc.intern.entity.QrCode;
import com.dxc.intern.service.QrCodeService;
import com.dxc.intern.utils.QRCodeUtil;
import com.dxc.untils.result.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Random;

/**
 * <p>
 * 二维码(求职者扫码操作) 前端控制器
 * </p>
 *
 * @author zhangchengming
 * @since 2022-02-21
 */
@RestController
@RequestMapping("/qrCode")
public class QrCodeController {
    @Autowired
    QrCodeService qrCodeService;

    @PostMapping("addQrCode")
    public ResultUtil addQrCode(@RequestBody QrCode qrCode, HttpServletRequest request) throws UnsupportedEncodingException {

        String url="";
        String addr=url+"?id=";
        QrCode qrCodelast=qrCodeService.getlast();
        if(qrCodelast==null){
            addr=addr+1;
        }
        else{
            Random r = new Random();
            int i1 = r.nextInt(10000);
            addr=addr+i1;
        }
        qrCode.setQrStatus(1);
        qrCode.setQrAddr(addr);
        String base="data:img/jpg;base64,"+(new QRCodeUtil()).getBase64(addr);
        qrCode.setQrBase64(base);
        qrCodeService.addQrCode(qrCode);

        return ResultUtil.success("添加成功","添加成功");
    }

    @DeleteMapping("delQrCode")
    public ResultUtil delQrCode(@RequestParam("id") Integer id,HttpServletRequest request) throws UnsupportedEncodingException{
        qrCodeService.delQrCode(id);
        return ResultUtil.success("删除成功","删除成功");
    }

    @PostMapping("updateQrCode")
    public ResultUtil updateQrCode(@RequestBody QrCode qrCode,HttpServletRequest request) throws UnsupportedEncodingException{
        qrCodeService.updateQrCode(qrCode);
        return ResultUtil.success("更新成功","更新成功");
    }

    @PostMapping("getQrCodeByNameOrStatus")
    public ResultUtil getQrCodeByNameOrStatus(Long current, Long size,@RequestBody(required = false) QrCode qrCode,HttpServletRequest request) throws UnsupportedEncodingException{
        System.out.println(qrCode);
        return ResultUtil.success("查找成功",qrCodeService.getQrCodeByNameOrStatus(current,size,qrCode));
    }

    @GetMapping("getQrCodeById")
    public ResultUtil getQrCodeById(@RequestBody QrCode qrCode,HttpServletRequest request) throws UnsupportedEncodingException{

        String base="";
        if(qrCode.getQrStatus()==1){
            return ResultUtil.success("返回有效二维码",qrCodeService.getQrCodeById(qrCode.getId()));
        }
        else{
            return ResultUtil.success("返回无效二维码",base);
        }


    }

    @PostMapping("isVaild")
    public ResultUtil<Boolean> isVaild(@RequestParam("url") String url) throws UnsupportedEncodingException{
        Date date=new Date();
        QrCode qrCode=qrCodeService.getQrCodeByurl(url);
        if(qrCode.getQrStatus()==1 && date.before(qrCode.getQrExpire())){
            return ResultUtil.success("有效",true);
        }
        else{
            return ResultUtil.failed("失效");
        }

    }








}



